#include "cppTutorStaticCast.h"
#include <iostream>

using namespace std;

CcppTutorStaticCast :: CcppTutorStaticCast () {
    d = 3.14159265;
    npPtr = NULL;
}

CcppTutorStaticCast :: ~CcppTutorStaticCast () {
}

void CcppTutorStaticCast :: m_StaticCast () {
    cout << "ENTER : CcppTutorStaticCast :: m_StaticCast" << endl;

    cout << "d = " << d << endl;
    i = static_cast <int> (d);
    cout << "Performing conversion : i = static_cast <int> (d)" << endl;
    cout << "i = " << i << endl;
    vpPtr = &i;
    //cout << "*vpPtr = " << *vpPtr << endl;
    cout << "\nPerforming pointer conversion : npPtr = static_cast <int*> (vpPtr);" << endl;
    npPtr = static_cast <int*> (vpPtr);
    cout << "*npPtr = " << *npPtr << endl;

    cout << "EXIT  : CcppTutorStaticCast :: m_StaticCast" << endl;
}

CcppTutorStaticCastAnother :: CcppTutorStaticCastAnother () {
}

CcppTutorStaticCastAnother :: ~CcppTutorStaticCastAnother () {
}

void CcppTutorStaticCastAnother :: m_StaticCastAnother () {
    cout << "ENTER : CcppTutorStaticCastAnother :: m_StaticCastAnother" << endl;
    cout << "EXIT  : CcppTutorStaticCastAnother :: m_StaticCastAnother" << endl;
}

void someFunc (CcppTutorStaticCast* sc, CcppTutorStaticCastAnother* sca) {
    cout << "ENTER : someFunc" << endl;

    // not safe
    CcppTutorStaticCastAnother* sca1 = static_cast <CcppTutorStaticCastAnother*> (sc);
    // result in an access violation, objet may not be fully constructed
    sca1->m_StaticCast (); 
    sca1->m_StaticCastAnother ();

    // safe conversion
    CcppTutorStaticCast* sc1         = static_cast <CcppTutorStaticCast*> (sca);
    sc1->m_StaticCast ();

    cout << "EXIT  : someFunc" << endl;
}